import {wx} from '../../../../weixin2alipay/index.js';
import {Page,Component} from '../../../../weixin2alipay/index.js';
import {compareVersion} from '../../../../util/util';
Page({
    "onShareAppMessage"(){
      return {
        "title":'canvas',
        "path":'page/component/pages/canvas/canvas'
};
    },
    "data":{
        "canIUse":true
},
    "onReady"(){
      const {SDKVersion} = wx.getSystemInfoSync();
      if(compareVersion(SDKVersion,'2.7.0') < 0)
      {
        console.log('123');
        this.setData({
            "canIUse":false
});
      }
else
      {
        this.position = {
            "x":150,
            "y":150,
            "vx":2,
            "vy":2
};
        this.drawBall();
        this.interval = setInterval(this.drawBall,17);
      };
    },
    "render"(canvas,ctx){
      ctx.clearRect(0,0,305,305);
      this.drawBall2D(ctx);
      this.drawCar(ctx);
    },
    "drawBall"(){
      const p = this.position;
      p.x += p.vx;
      p.y += p.vy;
      if(p.x >= 300)
      {
        p.vx = - 2;
      };
      if(p.x <= 7)
      {
        p.vx = 2;
      };
      if(p.y >= 300)
      {
        p.vy = - 2;
      };
      if(p.y <= 7)
      {
        p.vy = 2;
      };
      const context = wx.createCanvasContext('canvas');
      function ball(x,y){
      context.beginPath(0);
      context.arc(x,y,5,0,Math.PI * 2);
      context.setFillStyle('#1aad19');
      context.setStrokeStyle('rgba(1,1,1,0)');
      context.fill();
      context.stroke();
    };
      ball(p.x,150);
      ball(150,p.y);
      ball(300 - p.x,150);
      ball(150,300 - p.y);
      ball(p.x,p.y);
      ball(300 - p.x,300 - p.y);
      ball(p.x,300 - p.y);
      ball(300 - p.x,p.y);
      context.draw();
    },
    "onUnload"(){
      clearInterval(this.interval);
    }
});

